{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "1ebe8cdd",
   "metadata": {},
   "source": [
    "# 本周内容\n",
    "* 1.字符串\n",
    "* 2.列表"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ec78094c",
   "metadata": {},
   "source": [
    "## 1.字符串\n",
    "* 关于字符串的基本内容\n",
    "* 关于字符串的方法和函数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "89e83624",
   "metadata": {},
   "source": [
    "### 1.1关于字符串的基本内容"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a2cd5b6d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'span eggs'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'span eggs'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "3bf5c31c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'str'>\n"
     ]
    }
   ],
   "source": [
    "print(type('span eggs'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "f58e4437",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (2624006799.py, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Input \u001b[1;32mIn [4]\u001b[1;36m\u001b[0m\n\u001b[1;33m    'dosen't'\u001b[0m\n\u001b[1;37m           ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "'dosen't'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "e2ccc208",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"doesn't\""
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 以上解决方案\n",
    "\"doesn't\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "c0672082",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'doesn/t'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'doesn/t'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "fca334d5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 三引号：保留字符串格式信息的格式信息,保留完整的数据信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "60eb6825",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"doesn't\""
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'''doesn't'''"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c7a99136",
   "metadata": {},
   "source": [
    "### 1.2关于字符串的方法及函数\n",
    "> 1. 字符串是可以拼接的(字符串的运算)\n",
    "> 2. 字符串是有顺序的,从第一个字符开始，从0依次往后，有长度,可以用len检查长度\n",
    "> 3. 字符串的取值\n",
    ">> 1. 取单个值：从左往右（正数，第一个值是0），从右往左(负数，第一个值是-1）\n",
    ">> 2. 取多个值：切片的方法\n",
    "> 4. 字符串的方法及函数\n",
    ">> 1. in查询成员是否存在\n",
    ">> 2. str.count()查询数量\n",
    ">> 3. str.isdigit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "35f7a3c4",
   "metadata": {},
   "outputs": [],
   "source": [
    "name=[\"小明\",\"小红\",\"李华\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "2e5d3998",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "广州南方学院团委欢迎小明\n",
      "广州南方学院团委欢迎小红\n",
      "广州南方学院团委欢迎李华\n"
     ]
    }
   ],
   "source": [
    "for i in name:\n",
    "    print(\"广州南方学院团委欢迎\"+i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "8a1e200e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'广州南方学院团委欢迎小明'"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text='广州南方学院团委欢迎小明'\n",
    "text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "698a6a17",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'广'"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "7d420ad4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'州'"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "a316ddd3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'南'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "6f8e403d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "86434bbe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'团委'"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text[6]+text[7]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "dcce1187",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'团委'"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用切片的方法进行多个值取值\n",
    "text[6:8]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "26c5adee",
   "metadata": {},
   "source": [
    "* in 查询成员"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "309f710a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'广州' in text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "5cac16ef",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'北京' in text"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "629ffe55",
   "metadata": {},
   "source": [
    "* str.count()查询数量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "e9095d84",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text.count('广州')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "a048f72a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "19 <class 'int'>\n",
      "19 <class 'str'>\n"
     ]
    }
   ],
   "source": [
    "age_li=19\n",
    "age_wang='19'\n",
    "print(age_li,type(age_li))\n",
    "print(age_wang,type(age_wang))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "35946642",
   "metadata": {},
   "source": [
    "## 2.有序的数据结构——列表（list）\n",
    "* Python knows a number of compound data types, used to group together other values. The most versatile is the list, which can be written as a list of comma-separated values (items) between square brackets. Lists might contain items of different types, but usually the items all have the same type.\n",
    "> 1. 列表的基本运算法则（参考：有序的数据结构的运算法则列表)\n",
    "> 2. 列表的一般方法(要求：全部掌握，并且做总结在课后笔记中)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "d31efe00",
   "metadata": {},
   "outputs": [],
   "source": [
    "网二_name =\"李华,小明,李晓华,小红\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "42b12e42",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'李华'"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网二_name[0:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "a739ba62",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'小明'"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网二_name[3:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "350a5d64",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'李晓华'"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网二_name[6:9]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "05fe211e",
   "metadata": {},
   "source": [
    "* 对比字符串和列表\n",
    "> 1. 字符串是按字符进行数据取值\n",
    "> 2. 列表是按照元素进行数据取值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "7a283dc2",
   "metadata": {},
   "outputs": [],
   "source": [
    "网二_name_list=[\"李华\",\"小明\",\"李晓华\",\"小红\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "f8ac6947",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(网二_name_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "1590d092",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'李华'"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网二_name_list[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "cf4cca0e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小明', '李晓华']"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网二_name_list[-3:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "2e57cb78",
   "metadata": {},
   "outputs": [],
   "source": [
    "网二_name_list=[19,18,19,20]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "1e1290fb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "19"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网二_name_list[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "2824f7dd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[19, 18, 19, 20]"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网二_name_list[:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "2406b25e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[19,\n",
       " 18,\n",
       " 19,\n",
       " 20,\n",
       " 19,\n",
       " 18,\n",
       " 19,\n",
       " 20,\n",
       " 19,\n",
       " 18,\n",
       " 19,\n",
       " 20,\n",
       " 19,\n",
       " 18,\n",
       " 19,\n",
       " 20,\n",
       " 19,\n",
       " 18,\n",
       " 19,\n",
       " 20]"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网二_name_list*5"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "441b9509",
   "metadata": {},
   "source": [
    "### 2.2列表的方法\n",
    "> 1. 增加\n",
    "> 2. 删除\n",
    "> 3. 修改\n",
    "> 4. 查询 in，count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "b3fc2bea",
   "metadata": {},
   "outputs": [],
   "source": [
    "names =[\"mike\",\"marry\",\"jan\",\"jack\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "b4bcc47f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function append:\n",
      "\n",
      "append(object, /) method of builtins.list instance\n",
      "    Append object to the end of the list.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 学习方法 .tab键\n",
    "help(names.append)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "8fa32efa",
   "metadata": {},
   "outputs": [],
   "source": [
    "names.append(\"zhichao\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "0bc1e089",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['mike', 'marry', 'jan', 'jack', 'zhichao']"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "f91fa2d2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function insert:\n",
      "\n",
      "insert(index, object, /) method of builtins.list instance\n",
      "    Insert object before index.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(names.insert)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "060372cd",
   "metadata": {},
   "outputs": [],
   "source": [
    "names.insert(1,\"alex\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "a467d08b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['mike', 'alex', 'alex', 'marry', 'jan', 'jack', 'zhichao']"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "923f57e3",
   "metadata": {},
   "source": [
    "# 课后练习（做一个表格，总结列表的方法）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "25ce9334",
   "metadata": {},
   "source": [
    "列表方法| Description |Text Text\n",
    ":-: | :-: | :-: \n",
    "append()| 增加元素，增加在列表的末尾 | names.append(\"alei\").['mike', 'marry', 'jan', 'jack', 'alei']\n",
    "copy()   | 把前面的列表复制一遍     | names.cler(),['mike', 'marry', 'jan', 'jack', 'alei']\n",
    "clear()|删除所有项目|\n",
    "extend()|合并列表|names.extend(\"ben\")names\n",
    "insert(index,object)|向指定位置添加元素|names.insert(2,\"ben\")names,['mike', 'marry','ben' 'jan', 'jack']\n",
    "pop()|从列表里搜索元素|name.pop(0),\"mike\"\n",
    "pop()|删除一个元素|list.remove(12)print(list),[19,18,19,20,18]\n",
    "reverse|讲列表进行翻转|list.reverse()print(list),[18,20,19,18,19]\n",
    "index()|查找元素的位置，返回下标值|print(list.index(20)),3\n",
    "sort()|列表中所有元素按照大到小排列|list.sort()print(list),[18,18,19,19,20]\n",
    "count()|统计元素出现的次数|list.count(18),2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a4e7c764",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
